/**
 * 用户操作相关API封装
 * 解决模块导入问题的备用方案
 */

import type { ApiResponse } from './types/common'
import { http } from '@/utils/request'

// 用户关注相关类型定义
export interface UserFollowListRespVO {
  userId: number
  nickname?: string
  avatar?: string
  followTime?: string
  status?: string
  isMutualFollow?: boolean
  stats?: {
    followingCount?: number
    followerCount?: number
    punchCount?: number
    readingCount?: number
  }
}

/**
 * 关注用户
 */
export async function followUser(userId: number): Promise<{
  code: number
  data: boolean
  message?: string
}> {
  const response = await http.post<ApiResponse<boolean>>(`/reading/follow/follow/${userId}`)
  return {
    code: response.code || 0,
    data: response.data || false,
    message: response.message,
  }
}

/**
 * 取消关注用户
 */
export async function unfollowUser(userId: number): Promise<{
  code: number
  data: boolean
  message?: string
}> {
  const response = await http.delete<ApiResponse<boolean>>(`/reading/follow/unfollow/${userId}`)
  return {
    code: response.code || 0,
    data: response.data || false,
    message: response.message,
  }
}

/**
 * 获取关注列表
 */
export async function getFollowingList(limit?: number): Promise<{
  code: number
  data: UserFollowListRespVO[]
  message?: string
}> {
  const params = limit ? { limit } : {}
  const response = await http.get<ApiResponse<UserFollowListRespVO[]>>('/reading/follow/following', params)

  // 确保返回格式一致
  return {
    code: response.code || 0,
    data: response.data || [],
    message: response.message,
  }
}

/**
 * 检查关注状态
 */
export async function checkFollowStatus(userId: number): Promise<{
  code: number
  data: boolean
  message?: string
}> {
  const response = await http.get<ApiResponse<boolean>>(`/reading/follow/check/${userId}`)
  return {
    code: response.code || 0,
    data: response.data || false,
    message: response.message,
  }
}

/**
 * 注销用户账号（永久删除）
 */
export async function deleteUserAccount(data: { password: string, reason?: string }) {
  return http<ApiResponse<boolean>>({
    url: '/reading/user/account',
    method: 'DELETE',
    data,
  })
}

/**
 * 用户登出
 */
export async function userLogout() {
  return http.post<ApiResponse<boolean>>('/reading/user/logout')
}
